<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
  <h1>{{c.gradeLevel}}{{c.name}} 班主任：{{c.teacherName}}</h1>
    <input type="text" placeholder="学生姓名" v-model="s.name">
    <input type="text" placeholder="学生年龄" v-model="s.age">
    <input type="text" placeholder="学生性别" v-model="s.gender">
    <input type="button" value="添加学生" @click="insert()">

    <table border="1">
        <caption>学生列表</caption>
        <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>操作</th>
        </tr>
        <tr v-for="(s,i) in arr">
            <td>{{i+1}}</td>
            <td>{{s.name}}</td>
            <td>{{s.age}}</td>
            <td>{{s.gender}}</td>
            <td><a href="javascript:void(0)" @click="del(s.id)">删除</a></td>
        </tr>
    </table>
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<script>
  let v = new Vue({
    el:"body>div",
    data:{
        c: {name: "", teacherName: "", gradeLevel: ""},
        s: {name: "", age: "", gender: "", classId: ""},
        arr: []
    },
    methods:{
        del(id) {
            if (confirm("确认删除？")) {
                axios.post("/student/deleteById?id=" + id).then(function () {
                    alert("删除成功！");
                    location.reload();
                })
            }
        },
        insert() {
            // v.s.classId = new URLSearchParams(location.search).get("id");
            v.s.classId = v.c.id;
            axios.post("/student/insert",v.s).then(function () {
                alert("添加完成！");
                location.reload();
            })
        }
    },
    created: function () {
        axios.get("/class/selectById" + location.search).then(function (response) {
            v.c = response.data;
        })

        // axios.get("/student/select").then(function (response) {
        //     v.arr = response.data;
        // })

        axios.get("/student/selectByCid"+location.search).then(function (response) {
            v.arr = response.data;
        })
    }
  })
</script>
</html>